package com.gddb.xwj.mywiki.controller;

import com.gddb.xwj.mywiki.req.CategoryQueryReq;
import com.gddb.xwj.mywiki.req.CategorySaveReq;
import com.gddb.xwj.mywiki.resp.CategoryQueryResp;
import com.gddb.xwj.mywiki.resp.CommonResp;
import com.gddb.xwj.mywiki.resp.PageResp;
import com.gddb.xwj.mywiki.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 描述：分类管理
 *
 * @author xwj
 * @create 2022-03-18 19:17
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 全量查询 GET请求是form提交方式 方法参数不用加@RequstBody
     * @return
     */
    @GetMapping("/all")
    public CommonResp all(CategoryQueryReq req) {
        CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
        List<CategoryQueryResp> list = categoryService.all(req);
        resp.setContent(list);
        return resp;
    }

    /**
     * 分页查询
     * @param req
     * @return
     */
    @GetMapping("/list")
    public CommonResp list(CategoryQueryReq req){
        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
        PageResp<CategoryQueryResp> cont = categoryService.list(req);
        resp.setContent(cont);
        return resp;
    }

    /**
     * 保存
     * @param req
     * @return
     */
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody CategorySaveReq req){
        CommonResp resp = new CommonResp<>();
        categoryService.save(req);
        return resp;
    }

    /**
     * /{id} 用 @PathValiable接收
     * JSON 用 @RequestBody接收
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id){
        CommonResp<Object> resp = new CommonResp<>();
        categoryService.delete(id);
        return resp;
    }
}
